/**
 * Created by fangxing on 2016/11/30.
 */

/**
 * tabMainName
 * tabName
 * icon
 * tabTitle
 * content
 * tabUrl
 * tabContentMainName
 * @param options
 */
function addTab(options) {
    var nojump=new Array('mjkgl','fqgl','assign_barCode','assign_cBarCode',"network_account_management","write_off_network_management","manual_cost_management",'network_recharge_management','headquarters_financial_statement','basic_data_maintenance','account_information')
    if(in_array(options.tabName,nojump)){
        return false;
    }

    var exists = checkTabIsExists(options.tabMainName, options.tabName);

    if(exists)
    {
        $("#tab_a_"+options.tabName).click();
    }else{
        $("#"+options.tabMainName).append('' +
            '<li id="tab_li_'+options.tabName+'">' +
                '<a href="#tab_content_'+options.tabName+'" data-toggle="tab" id="tab_a_'+options.tabName+'">' +
                    // '<span class="icon-wrapper"><i class="fa ' + options.icon + '"></i></span>' +
                    '<span class="text-wrapper">' + options.tabTitle + '</span>' +
                    '&nbsp;<i class="icon-remove" onclick="closeTab(this);"></i></a></li>');
        var content = "";
        if(options.content)
        {
            content = options.content;
        }
        $("#"+options.tabContentMainName).append('<div id="tab_content_'+options.tabName+'" class="tab-pane fade" id="'+options.tabName+'">'+content+'</div>');
        $("#tab_a_"+options.tabName).click();
        if(options.tabUrl)
        {
            $("#tab_content_" + options.tabName + " iframe").attr("src", options.tabUrl)
        }
    }
}
function in_array(needle, haystack) {
    var i = 0, n = haystack.length;

    for (;i < n;++i)
        if (haystack[i] === needle)
            return true;

    return false;
}
function closeTab (button) {
    //通过该button找到对应li标签的id
    var li_id = $(button).parent().parent().attr('id');
    var id = li_id.replace("tab_li_","");
    //如果关闭的是当前激活的TAB，激活他的前一个TAB
    if ($("#tab-header li.active").attr('id') == li_id) {
        var pre = $("#tab-header li.active").prev().find("a");
        var pre_id = pre.attr("href");
        pre.click();
        $(pre_id).addClass("in active");
    }
    //关闭TAB
    $("#" + li_id).remove();
    $("#tab_content_" + id).remove();
}

function checkTabIsExists(tabMainName, tabName) {
    var tab = $("#"+tabMainName+" > #tab_li_"+tabName);
    return tab.length > 0;
}

$(function () {
    $(".sidebar-menu .accordion .submenu li:not(.open) a").click(function (event) {
        event.preventDefault();
        var href = $(this).attr("href");
        if (href == '#') {
        	return false;
        }
        if(height == undefined) {
            /*var height = $(".sidebar-menu").height() - $("#tab-header").height() -100;*/
        var height= $(parent.document).height()-115;

            //console.log(height)

        }
        var options = {};
        options.tabUrl = $(this).attr("href");
        options.tabTitle = $(this).attr("title");
        options.tabName  = $(this).attr("id");
        options.tabMainName = "tab-header";
        options.tabContentMainName = "tab-body";
        options.icon = $(this).attr("icon");
        options.content = "<iframe id=\"dd\" width=\"100%\" height=' "+height+" ' frameborder=\"0\"></iframe>"
        parent.addTab(options);
    });
});


